import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.Range;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class Word {

    // 读取xls文件路径
    static String pathname = "E:\\yg\\xlsx\\YG12_杭州营悄悄话(1).xls";
    // 模版路径
    static String templateDoc = "E:\\yg\\omi template.doc";
    // 悄悄话从第几列开始、结束
    static int first = 4;
    static int end = 129;
    // 人从第几排开始、结束
    static int firstRow = 1;
    static int endRow = 111;
    // 名称所在列
    static int nameColumn = 1;
    // 标示匿名所在列
    static int unNameColumn = 3;
    // 是什么标示该用户是否匿名
    static String discriminate = "需要呀";
    // 生成文件路径文件夹
    static String generatePath = "E:\\yg\\生成\\杭州\\杭州12\\";

    static boolean isNamePoc = false;

    //static int pageOneRowNum = 28;
    //static int pageRowNum = 30;

    public static void main(String[] args) throws IOException, BiffException {
        // 打开xls
        File file = new File(pathname);
        Workbook workbook = Workbook.getWorkbook(file);
        Sheet sheet = workbook.getSheet(0);

        for (int i = first; i < end; i++) {
            // 打开word
            InputStream is = new FileInputStream(templateDoc);
            HWPFDocument doc = new HWPFDocument(is);
            OutputStream os = null;
            Range range = doc.getRange();
            range.replaceText("\b", "");
            Cell[] column = sheet.getColumn(i);
            String name = quchuInt(column[0].getContents());
            if (isNamePoc) {
                name = namePoc(name);
            }
            // 遍历所有的悄悄话
            for (int j = firstRow; j <= endRow; j++) {
                if (j > column.length - 1) {
                    // 删除
                    range.replaceText("${" + j + ".name}", "");
                    range.replaceText("${" + j + ".word}\r", "");
                    continue;
                }
                // 留言者姓名
                String papaName = sheet.getCell(nameColumn, j).getContents();
                if (discriminate.equals(sheet.getCell(unNameColumn, j).getContents())) {
                    papaName = "来自一位爱你的趴趴";
                }
                // 留言者说的话
                String contents = column[j].getContents().trim();

                if (isNotBlank(contents)) {
                    //替换
                    range.replaceText("${" + j + ".name}", papaName);
                    String pValue = contents + "\r";
                    int countLine = countLines(pValue);
                    int i1 = countLine + 2;
//                    pageOneRowNum -= i1;
//                    if (pageOneRowNum <= 0) {
//                        pageOneRowNum = pageRowNum;
//                    }
//                    if (i1 > pageOneRowNum) {
//
//                    }else if (pageOneRowNum < 3) {
//                        pValue = pValue + "\r" + "\r";
//                        pageOneRowNum = pageRowNum;
//                    }
                    range.replaceText("${" + j + ".word}", EmojiFilter.filterEmoji(pValue));

                } else {
                    // 删除
                    range.replaceText("${" + j + ".name}", "");
                    range.replaceText("${" + j + ".word}\r", "");
                }
            }
            range.replaceText("${name}", name);
            range.replaceText("# \r", "");
            String docName = generatePath + name + ".doc";
            os = new FileOutputStream(new File(docName));
            //把doc输出到输出流中
            doc.write(os);
            // 复位
            //pageOneRowNum = 28;
            System.out.println(name + "输出成功");
        }

    }

    private static String namePoc(String name) {
        if (name.contains("、")){
            name = name.split("、")[1];
        }
        if (name.contains("【")) {
            name = name.split("【")[0];
        }
        if (name.contains("|")) {
            name = name.replace('|', '&');
        }
        return name;
    }

    private static String quchuInt(String text) {
        if (isNotBlank(text)) {
            text = text.replaceFirst("^[0-9]*", "");
            String[] split = text.split("/");
            return split[0].replace("\t", "");
        }
        return "";
    }

    public static boolean isBlank(String str) {
        int strLen;
        if (str != null && (strLen = str.length()) != 0) {
            for (int i = 0; i < strLen; ++i) {
                if (!Character.isWhitespace(str.charAt(i))) {
                    return false;
                }
            }

            return true;
        } else {
            return true;
        }
    }

    public static boolean isNotBlank(String str) {
        return !isBlank(str);
    }

    /**
     * 计算行数
     *
     * @param str
     * @return
     */
    private static int countLines(String str) {
        //String[] lines = str.split("\r\n|\r|\n");
        int i = str.length() / 45;
        return i + 1;

    }

}
